<template>
	<view class="pics">
		<scroll-view class="left" scroll-y>
			<!-- :class="active===index ? 'active' : '' " 当active的值等于index的时候给他加active的类名 -->
			<view class="item" v-for="(item,index) in titlelist" :key="item.id" @click="getPics(index,item.id)"
				:class="active===index ? 'active' : '' ">
				{{item.title}}
			</view>
		</scroll-view>
		<scroll-view class="right" scroll-y>
			<view class="picdata" v-for="(item,index) in rightPic" :key="item.id">
				<image src='https://img0.baidu.com/it/u=1472391233,99561733&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500'>
				</image>
				<text>{{item.title}}</text>
			</view>
			<view v-if="rightPic.length===0">暂无数据</view>
		</scroll-view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				titlelist: [],
				rightPic: [],
				active: 0
			};
		},
		onLoad() {
			this.getPicsList()
		},
		methods: {
			// 打开界面时就需要加载这个函数
			async getPicsList() {
				const res = await this.$myRequest({
					url: '/api/getimgcategory'
				})
				this.titlelist = res.data.message
				// console.log(this.titlelist[0].id);
				// 在初次获取类别的时候就调用图片的接口，进入时是第一位0，把第一类别的id传递过去
				this.getPics(0, this.titlelist[0].id)
			},
			// 当点击类别的时候请求接口
			async getPics(index, id) {
				// 把当前点击的类别index值传递给active,然后active就跟index相等了
				this.active = index
				const res = await this.$myRequest({
					url: '/api/getimages/' + id
				})
				this.rightPic = res.data.message
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
	}

	.pics {
		height: 100%;
		display: flex;

		.left {
			height: 100%;
			width: 220rpx;

			.item {
				min-width: 220rpx;
				max-width: 220rpx;
				height: 60px;
				line-height: 60px;
				text-align: center;
				border-top: 1px solid #eee;
			}

			.active {
				background: $shop-color;
				color: #fff;
			}
		}

		.right {
			max-width: 500rpx;
			margin-left: 20rpx;
			margin-top: 10rpx;

			.picdata {
				margin-bottom: 20rpx;
				font-size: 30rpx;
				line-height: 1.5;

				image {
					width: 100%;
					margin-bottom: 10rpx;
					border-radius: 20rpx;
				}
			}

		}
	}
</style>
